package com.wendy.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author zhuwending
 */
@Service
@Slf4j
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 写入缓存
     *
     * @param key   key
     * @param value value
     * @return boolean
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 写入缓存并设置过期时间
     *
     * @param key        key
     * @param value      value
     * @param expireTime 过期时间
     * @return boolean
     */
    @SuppressWarnings("unchecked")
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 批量删除对应的value
     *
     * @param keys keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern pattern
     */
    @SuppressWarnings("unchecked")
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys != null && keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 删除对应的value
     *
     * @param key key
     */
    @SuppressWarnings("unchecked")
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断key是否存在
     *
     * @param key key
     * @return boolean
     */
    @SuppressWarnings("unchecked")
    public boolean exists(final String key) {
        Boolean result = redisTemplate.hasKey(key);
        return result == null ? false : result;
    }

    /**
     * 读取key值
     *
     * @param key key
     * @return key值
     */
    public Object get(final String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * hash添加
     *
     * @param key     key
     * @param hashKey 键
     * @param value   值
     */
    @SuppressWarnings("unchecked")
    public void hashSet(String key, Object hashKey, Object value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    /**
     * hash获取
     *
     * @param key     key
     * @param hashKey 键
     * @return 值
     */
    @SuppressWarnings("unchecked")
    public Object hashGet(String key, Object hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }

    /**
     * list添加
     *
     * @param key   key
     * @param value value
     */
    @SuppressWarnings("unchecked")
    public void listPush(String key, Object value) {
        redisTemplate.opsForList().rightPush(key, value);
    }

    /**
     * 获取list
     *
     * @param key   key
     * @param start 开始位置
     * @param stop  结束位置
     * @return list
     */
    @SuppressWarnings("unchecked")
    public List<Object> listRange(String key, Long start, Long stop) {
        if (start == null) {
            start = 0L;
        }
        if (stop == null) {
            stop = redisTemplate.opsForList().size(key);
            stop = stop == null ? 0 : stop;
        }
        return redisTemplate.opsForList().range(key, start, stop);
    }

    /**
     * set添加
     *
     * @param key   key
     * @param value value
     */
    @SuppressWarnings("unchecked")
    public void setAdd(String key, Object value) {
        redisTemplate.opsForSet().add(key, value);
    }

    /**
     * 获取set
     *
     * @param key key
     * @return set
     */
    @SuppressWarnings("unchecked")
    public Set<Object> setMembers(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    /**
     * 有序集合添加元素
     *
     * @param key   key
     * @param value 键
     * @param score 值
     */
    @SuppressWarnings("unchecked")
    public void zSetAdd(String key, Object value, double score) {
        redisTemplate.opsForZSet().add(key, value, score);
    }

    /**
     * 有序集合获取元素
     *
     * @param key key
     * @param min 最小值
     * @param max 最大值
     * @return 有序集合
     */
    @SuppressWarnings("unchecked")
    public Set<Object> rangeByScore(String key, double min, double max) {
        return redisTemplate.opsForZSet().rangeByScore(key, min, max);
    }
}
